package com.tw.wechart2.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.tw.wechart2.listener.RecyclerStateListener;
import com.tw.wechart2.util.MyLogUtils;

public class RecyclerView2 extends RecyclerView {
    private String TAG = RecyclerView2.class.getSimpleName();
    // 现行布局对象
    private LinearLayoutManager manager;
    // 初始点击位置
    private float startY = 0;
    // 是否需要刷新数据
    private boolean isNeddRefresh = false;
    // RecyclerView监听器
    private RecyclerStateListener listener;
    // 数据展示条数
    private int size = 0;

    public RecyclerView2(Context context) {
        super(context);
    }

    public RecyclerView2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RecyclerView2(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {

        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startY = motionEvent.getRawY();
//                MyLogUtils.instance().logI(TAG, "初始位置: " + startY);
                break;
            case MotionEvent.ACTION_MOVE:
                float currentY = motionEvent.getRawY();
                // 下拉效果实现
                if (manager.findFirstCompletelyVisibleItemPosition() == 0) {
                    if (currentY > startY) {
                        if (!isNeddRefresh) {
                            startY = currentY;
                        }
                        isNeddRefresh = true;
                        float currentDistance = currentY - startY;
                        // 下拉效果
                        this.setY(currentDistance);
                        MyLogUtils.instance().logI(TAG, "位移位置: " + currentDistance);
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                startY = 0;
                if (isNeddRefresh) {
                    isNeddRefresh = false;
                    this.setY(startY);

                    // 下拉刷新数据
                    listener.refreshData();
                }

//                MyLogUtils.instance().logI(TAG, "结束了: " + startY);
        }
        return super.dispatchTouchEvent(motionEvent);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        return super.onInterceptTouchEvent(e);
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        return super.onTouchEvent(motionEvent);
    }

    /**
     * @Description: 初始化布局对象
     * @Author: lanteng 2020/9/21
     * @return
     */
    public void initLinearLayoutManager() {
        manager = (LinearLayoutManager) getLayoutManager();
    }

    public void setDataSize(int size) {
        this.size = size;
    }

    public void setOnStateListener(RecyclerStateListener listener) {
        this.listener = listener;
    }

}
